package com.wwwarehouse.contract.adjust_contract;


import android.os.Bundle;
import android.support.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.carddesk.CardDeskMessageStableCardBean;
import com.wwwarehouse.common.fragment.CommonBasePagerFragment;
import com.wwwarehouse.common.tools.ToastUtils;
import com.wwwarehouse.contract.bean.TerminationOrderBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Creator by zhenwang.xiang on 2017/10/18 11:07
 * Description: 终止订单viewpager页面
 */

@Route(path = ContractConstant.PATH_TERMINATION_ORDER_VIEWPAGERFRAGMENT)
public class TerminationOrderViewPagerFragment extends CommonBasePagerFragment {
    private ArrayList<String> mBusinessIdList = new ArrayList<>();

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void requestDatas() {
        super.requestDatas();

        if (getArguments() != null) {
            CardDeskMessageStableCardBean.TasksBean tasksBean = getArguments().getParcelable("data");
            mBusinessIdList = (ArrayList<String>) tasksBean.getBusinessIds();
        }
        //网络请求
        requestHttp();

    }

    @Override
    public void setTitle() {
        super.setTitle();
        if (peekFragment() instanceof TerminationOrderViewPagerFragment) {
            mActivity.setTitle(getString(R.string.termination_order_title));
        }
    }

    @Override
    public void onDownPull() {

    }


    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("page", "1");
        map.put("size", "7");
        map.put("businessIds", mBusinessIdList);
        loadData(ContractConstant.GET_TERMINATIONS, map, 0);
    }


    @Override
    public void onRefreshListener() {
        requestHttp();
    }

    @Override
    public void onLoadSuccess(String data, int what) {
        if (0 == what) {
            TerminationOrderBean bean = JSON.parseObject(data, TerminationOrderBean.class);
            if (null != bean) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("businessIds", mBusinessIdList);
                try {
                    setData(bean.getTotal(), 7, "com.wwwarehouse.contract.adjust_contract.TerminationOrderFragment", bundle, bean.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onLoadError(String errorInfo) {
        ToastUtils.showToast(errorInfo);
    }
}
